var app = getApp();
// var util = require('../../utils/util.js');
// import WxValidate from '../../utils/validate';
//获取应用实例
var app = getApp();
var util = require('../../utils/util.js');
import WxValidate from '../../utils/validate';

var inputContent = {};//输入内容
Page({

    /**
     * 页面的初始数据
     */
    data: {
        currentTab: 1,   // tab切换
        paracont: "获取验证码",//验证码文字
        vcdisabled: true,//验证码按钮状态
        verifycode: ""//返回的验证码
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        // 页面初始化 options为页面跳转所带来的参数
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },
    /**
     * 获取用户输入
     */
    bindChange: function (e) {
        inputContent[e.currentTarget.id] = e.detail.value;
        util.verifyCodeBtn(e, this);
    },
    /**
     * 获取验证码
     */
    getVerifyCode: function (e) {
        var that = this;
        util.getVerifyCode(inputContent['user'], this, function (data) {
            that.setData({
                verifycode: data.data
            })
        })

    },
    /**
     * 登录提交
     */
    loginSubmit: function (e) {
         let userId = wx.getStorageSync("userId")
        var that = this;
        let tabId = e.currentTarget.dataset.tab
        //验证表单
        if (tabId == 1) {
            if (!e.detail.value.account) {
                wx.showToast({
                    title: '请输入手机号',
                    icon: 'none',
                })
                return false
            }
            if (!/^1[3456789]\d{9}$/.test(e.detail.value.account)) {
                wx.showToast({
                    title: '请输入正确手机号',
                    icon: 'none',
                })
                return false
            }
            if (!e.detail.value.password) {
                wx.showToast({
                    title: '请输入密码',
                    icon: 'none',
                })
                return false
            }
            if (e.detail.value.password.length < 6) {
                wx.showToast({
                    title: '密码不得小于6位',
                    icon: 'none',
                })
                return false
            }
            wx.request({
                url: app.globalData.api + '/app/auto/login', //上线的话必须是https，没有appId的本地请求貌似不受影响 
                method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT 
                header: {
                    'Content-Type': "application/json",
                    'Cookie': 'SESSION=' + wx.getStorageSync("sessionId")
                }, // 设置请求的 header
                data: {
                    password: e.detail.value.password,
                    username: e.detail.value.account,
                    userId: userId
                },
                success: function (res) {
                    //授权成功后，跳转进入小程序首页 
                    if (res.data.code == 200) {
                        wx.switchTab({
                            url: '/pages/index/index',
                            success: function (e) {
                                var page = getCurrentPages().pop();
                                if (page == undefined || page == null) return;
                                page.onLoad();
                            }
                        })
                        wx.setStorageSync("token", res.data.data.COOKIE_TOKEN)
                        wx.setStorageSync('teamPartnerStatus', res.data.data.info.teamPartnerStatus)
                        wx.setStorageSync('empClass', res.data.data.info.empClass)
                        wx.setStorageSync('isJoinTeam', res.data.data.info.isJoinTeam)
                        wx.setStorageSync('loginId', res.data.data.info.id)
                    } else {
                        wx.showModal({
                            title: '警告',
                            content: res.data.message,
                            showCancel: false,
                            confirmText: '重新登录',
                        })
                    }
                },
                fail: function () {
                },
                complete: function () {
                    // complete 
                }
            })
        } else if (tabId == 0) {
            if (!e.detail.value.user) {
                wx.showToast({
                    title: '请输入手机号',
                    icon: 'none',
                })
                return false
            }
            if (!/^1[3456789]\d{9}$/.test(e.detail.value.user)) {
                wx.showToast({
                    title: '请输入正确手机号',
                    icon: 'none',
                })
                return false
            }
            if (!e.detail.value.verifycode) {
                wx.showToast({
                    title: '请输入验证码',
                    icon: 'none',
                })
                return false
            }
                wx.request({
                    url: app.globalData.api + '/app/auto/codeLogin', //上线的话必须是https，没有appId的本地请求貌似不受影响 
                    method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT 
                    header: {
                        'Content-Type': "application/json",
                        'Cookie': 'SESSION=' + wx.getStorageSync("sessionId")
                    }, // 设置请求的 header
                    data: {
                        username: e.detail.value.user,
                        code: e.detail.value.verifycode,
                        userId: userId
                    },
                    success: function (res) {
                        //授权成功后，跳转进入小程序首页 
                        if (res.data.code == 200) {
                            wx.switchTab({
                                url: '/pages/index/index',
                                success: function (e) {
                                    var page = getCurrentPages().pop();
                                    if (page == undefined || page == null) return;
                                    page.onLoad();
                                }
                            })
                            wx.setStorageSync("token", res.data.data.COOKIE_TOKEN)
                            wx.setStorageSync('teamPartnerStatus', res.data.data.info.teamPartnerStatus)
                            wx.setStorageSync('empClass', res.data.data.info.empClass)
                            wx.setStorageSync('isJoinTeam', res.data.data.info.isJoinTeam)
                            wx.setStorageSync('loginId', res.data.data.info.id)
                        } else {
                            wx.showModal({
                                title: '警告',
                                content: res.data.message,
                                showCancel: false,
                                confirmText: '重新登录',
                            })
                        }
                    }
                })
        }
    },
    /**
     * 登录成功后
     */
    loginSucess: function (data) {
        wx.setStorageSync("userid", data.data.userid);
        wx.setStorageSync("usersecret", data.data.usersecret);
        //接口API授权 type 1.是公共授权  2.登录授权
        util.authorization(2, function () {
            //根据会员ID获取会员账号基本信息
            util.getUserInfo(function (data) {
                //返回上一页
                /*        wx.navigateBack({
                            delta: 1
                        })*/
                wx.reLaunch({
                    url: '../index/index'
                })

            })
        }, true);


    },
    /**
     * 微信授权登录
     */
    bindGetUserInfo: function (e) {
        if (e.detail.userInfo) {
            //用户按了允许授权按钮
            wx.login({
                success(res) {
                    if (res.code) {
                        //发起请求
                        wx.request({
                            url: app.globalData.api + '/app/wx/grant/' + res.code,
                            success(res) {
                                if (res.data.code == 200) {
                                    wx.setStorageSync("token", res.data.data.COOKIE_TOKEN)
                                    wx.setStorageSync('teamPartnerStatus', res.data.data.info.teamPartnerStatus)
                                    //授权成功后，跳转进入小程序首页
                                    wx.switchTab({
                                        url: '/pages/index/index',
                                        success: function (e) {
                                            var page = getCurrentPages().pop();
                                            if (page == undefined || page == null) return;
                                            page.onLoad();
                                        }
                                    })
                                } else {
                                    if (res.data.code == 405) {
                                        wx.setStorageSync("userId", res.data.data)
                                        wx.showModal({
                                            title: '提示',
                                            content: res.data.message,
                                            showCancel: true,
                                            confirmText: '去绑定',
                                            success: function (res) {
                                                if (res.confirm) {
                                                    wx.redirectTo({
                                                        url: '/pages/account/authorization'
                                                    })
                                                }
                                            }
                                        })
                                    } else {
                                        wx.showModal({
                                            title: '提示',
                                            content: res.data.message,
                                            showCancel: false,
                                            confirmText: '返回登录',
                                        })
                                    }
                                }
                            }
                        })


                    } else {
                        console.log('登录失败！' + res.errMsg)
                    }
                }
            })
        } else {
            //用户按了拒绝按钮
            wx.showModal({
                title: '警告',
                content: '您点击了拒绝授权，将无法进入小程序，请授权之后再进入!',
                showCancel: false,
                confirmText: '返回授权',
                success: function (res) {
                    if (res.confirm) {
                        console.log('用户点击了“返回授权”')
                    }
                }
            })
        }
    },
    /**
     * 点击tab切换
     */
    swichNav: function (e) {
        util.swichNav(e, this)
    },
    account: function () {
        wx.navigateTo({
            url: '/pages/account/accountinfo',
        })
    }
})